1 | 'use strict';
|
2 |
|
3 | var Path = require('path');
|
4 | var Should = require('should');
|
5 | var debug = require('debug')('ifnode:test:components');
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | var IFNode = require('../');
|
12 | var app = IFNode({
|
13 | project_folder: Path.resolve(__dirname, '../examples/components'),
|
14 | env: 'local',
|
15 | alias: 'cmpts'
|
16 | }).load();
|
17 |
|
18 | describe('Components', function() {
|
19 | describe('app.component(id: string)', function() {
|
20 | it('should initialize component', function() {
|
21 | app.component('first').should.be.an.Object();
|
22 | app.component('component-in-folder').should.be.a.String();
|
23 |
|
24 | app.component('ClassES5').should.be.a.Function();
|
25 | app.component('ClassES5Component').should.be.not.a.Function();
|
26 | });
|
27 |
|
28 | it('should append methods', function() {
|
29 | app.component('third').custom().should.be.equal('is_custom');
|
30 | });
|
31 |
|
32 | it('should load internal component modules', function() {
|
33 | Should.equal(app.component('can-load-internal/other'), 'can-load-internal/other');
|
34 | });
|
35 |
|
36 | it('should be equal to app attached components', function() {
|
37 | Should.equal(app.first, app.component('first'));
|
38 | });
|
39 |
|
40 | it('should throw component internal error', function() {
|
41 | try {
|
42 | IFNode({
|
43 | project_folder: Path.resolve(__dirname, '../examples/components_with_error'),
|
44 | }).load();
|
45 | } catch(error) {
|
46 | Should.equal(error.message, 'Unexpected component error');
|
47 | }
|
48 | });
|
49 | });
|
50 |
|
51 | describe('app.Component(custom_component_config?: Object)', function() {
|
52 | it('should exists', function () {
|
53 | app.Component.should.be.a.Function();
|
54 | });
|
55 |
|
56 | describe('should throw duplicating', function() {
|
57 | it('of components names', function() {
|
58 | (function() {
|
59 | IFNode({
|
60 | project_folder: Path.resolve(__dirname, '../examples/components_with_error'),
|
61 | environment: 'names-duplicated'
|
62 | }).load();
|
63 | }).should.throw();
|
64 | });
|
65 | it('of application property by name', function() {
|
66 | (function() {
|
67 | IFNode({
|
68 | project_folder: Path.resolve(__dirname, '../examples/components_with_error'),
|
69 | environment: 'name-duplicated-app-prop'
|
70 | }).load();
|
71 | }).should.throw();
|
72 | });
|
73 | it('of application property by alias', function() {
|
74 | (function() {
|
75 | IFNode({
|
76 | project_folder: Path.resolve(__dirname, '../examples/components_with_error'),
|
77 | environment: 'alias-duplicated-app-prop'
|
78 | }).load();
|
79 | }).should.throw();
|
80 | });
|
81 | });
|
82 |
|
83 | it('default options', function() {
|
84 | app.components.first.config.value.should.be.equal(1);
|
85 | app.components.second.config.should.be.equal('value');
|
86 | app.components.third.config.should.have.properties({});
|
87 | });
|
88 |
|
89 | it('application instance options', function() {
|
90 | app.first.config.value.should.be.equal(1);
|
91 | app.second.config.should.be.equal('value');
|
92 | app.third.config.should.have.properties({});
|
93 | });
|
94 | });
|
95 |
|
96 | describe('Class "Component" based components', function() {
|
97 | describe('ES5', function() {
|
98 | it('should has static and plain methods', function() {
|
99 | var ClassES5 = app.component('ClassES5');
|
100 |
|
101 | Should.equal(ClassES5.STATIC, 'ClassES5.STATIC');
|
102 | Should.equal(ClassES5.getStatic(), 'ClassES5.STATIC');
|
103 | Should.equal(ClassES5.getName(), 'ClassES5');
|
104 |
|
105 | Should.equal((new ClassES5).plain(), 'ClassES5#plain');
|
106 | });
|
107 |
|
108 | it('should attach component from class', function() {
|
109 | Should.equal(app.ClassES5Component.plain(), 'ClassES5Component#plain');
|
110 | });
|
111 |
|
112 | it('should attach component from instance', function() {
|
113 | Should.equal(app.ClassES5ComponentInstance.plain(), 'ClassES5ComponentInstance#plain');
|
114 | });
|
115 |
|
116 | it('should attach component with several inherits', function() {
|
117 | Should.equal(app.ClassES5ComponentDoubleInherits.base_plain(), 'Base#base_plain');
|
118 | Should.equal(app.ClassES5ComponentDoubleInherits.plain(), 'ClassES5ComponentDoubleInherits#child_plain');
|
119 | });
|
120 | });
|
121 | });
|
122 |
|
123 | describe('component instance', function() {
|
124 | it('.initialize(config: Object)', function () {
|
125 | app.second.initialized.should.be.true();
|
126 | });
|
127 |
|
128 | it('component has alias', function() {
|
129 | app.second.should.be.equal(app.second_alias);
|
130 | Should.not.exist(app.third_alias);
|
131 | });
|
132 |
|
133 | it('custom method', function() {
|
134 | app.third.custom().should.be.equal('is_custom');
|
135 | });
|
136 | });
|
137 | });
|